package cn.mulanbay.job.handler;

import cn.mulanbay.api.system.bean.RemoteSystemLog;
import cn.mulanbay.api.system.service.RemoteLogService;
import cn.mulanbay.business.handler.BaseHandler;
import cn.mulanbay.business.handler.ThreadPoolHandler;
import cn.mulanbay.schedule.NotifiableProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class JobNotifiableHandler extends BaseHandler implements NotifiableProcessor {

    /**
     * 需要添加lazy，否则gateway无法正常启动，
     */
    @Lazy
    @Autowired
    RemoteLogService remoteLogService;

    @Autowired
    ThreadPoolHandler threadPoolHandler;

    public JobNotifiableHandler() {
        super("调度通知处理");
    }

    @Override
    public void notifyMessage(Long taskTriggerId, int code, String title, String message) {
        LocalDateTime date = LocalDateTime.now();
        threadPoolHandler.pushThread(() -> {
            RemoteSystemLog log = new RemoteSystemLog();
            log.setErrorCode(code);
            log.setTitle(title);
            log.setContent(message);
            log.setOccurTime(date);
            log.setRemark("调度编号:"+taskTriggerId);
            remoteLogService.addSystemLog(log);
        });
    }
}
